var Trim = require('../../utils/util.js');
var app;
var url;
var headers,touristid,index,initInfo;
var rxPhone = /^1[3|4|5|7|8][0-9]{9}$/;
var option;
var idReg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/; 
Page({
  data:{
    url:"https://app1.sztravel.com.cn/file/weixinapp/img",//图片总路径
    index:0,
    userinfo:{
      birthday:"", 
      card_no:"",
      card_type:"sfz",
      gender:"M",
      peer_id:null,
      phone_num:"",
      real_name:""
    },
    card_sort: ["身份证", "护照", "港澳通行证", "台湾通行证"],
    isActive:false,//是否可以提交开关
    isAdd:false//添加修改开关
  },
  isActive:function(){//判断提交按钮是否激活
    var isActive = this.data.isActive
    var info=this.data.userinfo;
    console.log(JSON.stringify(info) == JSON.stringify(initInfo))
    if (JSON.stringify(initInfo) != JSON.stringify(info) && info.birthday && info.card_no && info.phone_num && info.real_name){//暂定可以提交
      isActive=true;
    }else{
      isActive = false;
    }
    this.setData({
      isActive: isActive
    })
  },
  showMode:function(mes){
    wx.showModal({
      title: '来自伴行的提示',
      content: mes,
      showCancel:false
    })
  },
  goToChange:function(){//更改信息
    var isAdd=this.data.isAdd;
    var info = this.data.userinfo;
    var that=this;
    if (!info.real_name){//姓名不存在
      that.showMode("请填写姓名");
      return
    } else if (!info.birthday){//出生日期不存在
      that.showMode("请选择出生日期");
      return
    } else if (!info.phone_num) {//手机号码为空不存在
      that.showMode("请填写手机号码");
      return
    } else if (!info.card_no) {//证件号码为空不存在
      that.showMode("请填写证件号码");
      return
    } else if (!rxPhone.test(info.phone_num)) {//证件号码为空不存在
      that.showMode("手机号码有误");
      return
    } else if (!idReg.test(info.card_no) && info.card_type=="sfz"){
      that.showMode("身份证格式有误");
      return
    }
    if (this.data.isActive && !isAdd){//可以更改
      
      var req = '{"peer_id":'
        +touristid+',"phone_num":"'
        +info.phone_num+'","real_name":"'
        +info.real_name+'","gender":"'
        +info.gender+'","birthday":"'
        +info.birthday+'","card_type":"'
        +info.card_type+'","card_no":"'
        + info.card_no+'"}' 
      wx.request({
        url: url +'api/modify_peer_info',
        method:'POST',
        header:headers,
        data:{
          "req":encodeURIComponent(req)
        },
        success:function(data){
          if(data.data.result===1){
            wx.showModal({
              title: '来自伴行的提示',
              content:data.data.message,
              showCancel:false,
              success:function(){
                wx.navigateBack({
                  delta:1
                })
              }
            })
          }
        }
      })
    }else if(this.data.isActive && isAdd){//可以添加
      
      var req = '{"phone_num":"'
        + info.phone_num + '","real_name":"'
        + info.real_name + '","gender":"'
        + info.gender + '","birthday":"'
        + info.birthday + '","card_type":"'
        + info.card_type + '","card_no":"'
        + info.card_no + '"}';
      wx.request({
        url: url + 'api/add_peer',
        method: 'POST',
        header: headers,
        data: {
          "req": encodeURIComponent(req)
        },
        success: function (data) {
          if (data.data.result === 1) {
            wx.showModal({
              title: '来自伴行的提示',
              content: data.data.message,
              showCancel: false,
              success: function () {
                wx.navigateBack({
                  delta: 1
                })
              }
            })
          }
        }
      });
    }
  },
  sexselect:function(e){
    var sex=e.target.dataset.sex;
    var info=this.data.userinfo;
    info.gender = sex
    this.setData({
      userinfo:info
    })
    this.isActive()
  },
  bindPickerChange: function (e) {
    console.log(e.detail.value)
    var info=this.data.userinfo;
    switch (parseInt(e.detail.value)){
      case 0:
        info.card_type="sfz";break;
      case 1:
        info.card_type="passport"; break;
      case 2:
        info.card_type="ga"; break;
      case 3:
        info.card_type="tw"; break;
    };
    this.setData({
      index: e.detail.value,
      userinfo:info
    })
    this.isActive()
    // console.log(this.data.userinfo)
  },
  // bindDateChange:function(e){
  //   console.log(e)
  //   var info = this.data.userinfo;
  //   info.birthday=e.detail.value;
  //   this.setData({
  //     userinfo:info
  //   })
    
  // },
  fillter:function(e){
    var cdata = this.data.userinfo;
    // if(e.target.dataset.i=="sex"){
    //   cdata[e.target.dataset.i]=this.data.isman?"男":"女";
    // }else{
    //   cdata[e.target.dataset.i]=e.detail.value;
    // }
    // 
    // if(cdata.passport || cdata.id || cdata.gaid){
    //   cdata.isSave = true;
    // }else{
    //   cdata.isSave = false;
    // }
    console.log(e)
    // if(e.target.dataset.i=="tel"){//更改手机号码

    // }
   
    switch (e.target.dataset.i){
      case "name":
        cdata.real_name=e.detail.value;break;
      case "selectDate" :
        cdata.birthday=e.detail.value;break;
      case "tel"://更改手机号码
        cdata.phone_num=e.detail.value;break;
      case "card_no"://更改证件号码
        cdata.card_no = e.detail.value;break;
    };
    this.setData({
      userinfo:cdata
    })
    this.isActive()
    console.log(this.data.userinfo);
  },
  onLoad:function(options){
    // 页面初始化 options为页面跳转所带来的参数
    option = options.touristid
    var that = this;
    
    
   
  },
  onReady:function(){
    // 页面渲染完成
  },
  onShow:function(){
    // 页面显示
    app = getApp();
    url = app.globalData.url;
    headers = app.globalData.headers;
    var that=this;
    if (option) {//修改
      touristid = option;
      wx.request({
        url: url + 'api/get_peer_detail',
        header: headers,
        data: {
          "peer_id": touristid
        },
        success: function (data) {
          console.log(data);
          if (data.data.result === 1) {
            initInfo = data.data.data;//保留页面初始数据 若改变才可提交
            switch (data.data.data.card_type) {
              case "sfz":
                index = 0; break;
              case "ga":
                index = 2; break;
              case "tw":
                index = 3; break;
              case "passport":
                index = 1;
            }
            that.setData({
              userinfo: data.data.data,
              index: index,
              isAdd: false
            })
          } else {
            wx.showModal({
              title: '来自伴行的提示',
              content: data.data.message,
              showCancel: false
            })
          }
          console.log(that.data)
        }
      })
    } else {//添加
      this.setData({
        isAdd: true
      })
    }
  },
  onHide:function(){
    // 页面隐藏
  },
  onUnload:function(){
    // 页面关闭
  }
})